<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/6/12 0012
 * Time: 16:34
 */
namespace app\admin\controller;
use app\models\Models;

class Finance extends Check {
    /**
     * 充值列表
     * @return \think\response\View
     */
    public function rechargeList(){
        $get = input('param.');
        $order = new Models('order');
        // 条件查询
        $search = search($get,'a.order_no,b.id,b.nickname','a.create_time','a.status');
        $where  = $search['condition'];
        // 支付方式查询
        if($get['pay_method']){
            $where .= ' and a.pay_method = '.$get['pay_method'];
            $search['display']['pay_method'] = $get['pay_method'];
        }
        $join   = [
            ['user b','a.uid = b.id','left'],
            ['recharge_type c','a.pid = c.id','left']
        ];
        $where .= ' and a.type_id ='.Models::ORDER_TYPE_RECHARGE;
        $field  = "a.*,b.nickname,c.price,c.coin";
        $list = $order->_page($where,$field,'a.id desc',$join,$get['page'],15,$get);
        return view('rechargeList',['list'=>$list->items(),'page'=>$list->render(),'display'=>$search['display']]);
    }

    /**
     * 提现列表
     * @return \think\response\View
     */
    public function caseList(){
        $get = input('param.');
        $withdraw = new Models('withdraw');
        // 条件查询
        $search = search($get,'b.id,b.nickname','a.create_time','a.status');
        $where  = $search['condition'];
        $join   = [
            ['user b','a.uid = b.id','left']
        ];
        $field  = "a.*,b.nickname";
        $list = $withdraw->_page($where,$field,'a.id desc',$join,$get['page'],15,$get);
        return view('caseList',['list'=>$list->items(),'page'=>$list->render(),'display'=>$search['display']]);
    }

    /**
     * 审核通过
     * @return array
     */
    public function pass(){
        if(request()->isAjax()){
            $post = input('param.');
            $withdraw = new Models('withdraw');
            try{
                // 获取提现用户
                $info = $withdraw->_find($post['id']);
                // 开启事物
                $withdraw->startTrans();
                $bool = $withdraw->_save(['id'=>$post['id'],'status'=>Models::AUDIT_PASS,'pay_method'=>$post['pay_method'],'audit_time'=>time()]);
                // 账户相关表操作
                $account = new Models('account');
                $bool1 = $account->_save(['frozen'=>['exp','frozen-'.$info['money']]],['uid'=>$info['uid']]);
                if(!$bool || !$bool1){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                $withdraw->rollback();
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            $withdraw->commit();
            return self::formatSuccessResult();
        }
        $get = input('param.');
        return view('pass',['id'=>$get['id']]);
    }

    /**
     * 审核拒绝
     * @return array
     */
    public function nopass(){
        if(request()->isAjax()){
            $post = input('param.');
            $withdraw = new Models('withdraw');
            try{
                // 获取提现用户
                $info = $withdraw->_find($post['id']);
                // 开启事物
                $withdraw->startTrans();
                $bool = $withdraw->_save(['id'=>$post['id'],'status'=>Models::AUDIT_NOPASS,'audit_time'=>time()]);
                // 账户相关表操作
                $account = new Models('account');
                $bool1 = $account->_save(['frozen'=>['exp','frozen-'.$info['money']],'balance'=>['exp','balance+'.$info['money']]],['uid'=>$info['uid']]);
                if(!$bool || !$bool1){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                $withdraw->rollback();
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            $withdraw->commit();
            return self::formatSuccessResult();
        }
    }
}